package com.example.demo.controller;

import com.example.demo.controller.vo.ChangeGoodsStatusRequest;
import com.example.demo.controller.vo.BatchDeleteGoodsRequest;
import com.example.demo.entity.Goods;
import com.example.demo.entity.GoodsSkusCombinations;
import com.example.demo.entity.GoodsSkus;
import com.example.demo.service.GoodsService;
import com.example.demo.service.GoodsSkusCombinationsService;
import com.example.demo.service.GoodsSkusService;
import com.example.demo.controller.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsSkusService goodsSkusService;

    @Autowired
    private GoodsSkusCombinationsService  goodsSkuCombinationsService;

    @PostMapping
    public ResponseResult<Goods> createGoods(@RequestBody Goods goods) {
        Goods created = goodsService.createGoods(goods);
        return ResponseResult.success(created);
    }

    @PostMapping("/{id}")
    public ResponseResult<Boolean> updateGoods(@PathVariable Integer id, @RequestBody Goods goods) {
        goods.setId(id);
        boolean result = goodsService.updateGoods(goods);
        return ResponseResult.success(result);
    }

    @PostMapping("/changestatus")
    public ResponseResult<Integer> changeStatus(@RequestBody ChangeGoodsStatusRequest request) {
        int count = goodsService.changeStatus(request.getIds(), request.getStatus());
        return ResponseResult.success(count);
    }

    @GetMapping("/{page}")
    public ResponseResult<?> getGoodsPage(
            @PathVariable Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer category_id,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false, defaultValue = "false") Boolean onlyShowStockWarning) {
        Map<String, Object> result = goodsService.getGoodsPage(page, limit, title, category_id, status, onlyShowStockWarning);
        return ResponseResult.success(result);
    }

    @PostMapping("/delete_all")
    public ResponseResult<Integer> deleteGoods(@RequestBody BatchDeleteGoodsRequest request) {
        int count = goodsService.deleteGoods(request.getIds());
        return ResponseResult.success(count);
    }

    /**
     * 根据商品ID获取商品SKU列表
     *
     * @param id 商品ID
     * @return 包含商品SKU列表的响应结果
     */
    @GetMapping("/{id}/getSkus")
    public ResponseResult<List<GoodsSkus>> getGoodsSkusByGoodsId(@PathVariable Integer id) {
        List<GoodsSkus> goodsSkusList = goodsSkusService.getGoodsSkusWithValuesByGoodsId(id);
        return ResponseResult.success(goodsSkusList);
    }

    
    /**
     * 根据商品ID获取商品SKU组合列表
     *
     * @param id 商品ID
     * @return 包含商品SKU组合列表的响应结果
     */
    @GetMapping("/{id}/goods_skus_combinations")
    public ResponseResult<List<GoodsSkusCombinations>> getGoodsSkuCombinationsByGoodsId(@PathVariable Integer id) {
        List<GoodsSkusCombinations> goodsSkuCombinationsList = goodsSkuCombinationsService.getGoodsSkuCombinationsByGoodsId(id);
        return ResponseResult.success(goodsSkuCombinationsList);
    }
    /**
     * 根据商品ID获取商品详情
     *
     * @param id 商品ID
     * @return 包含商品详情的ResponseResult对象
     */
    @GetMapping("/{id}/detail")
    public ResponseResult<Goods> getGoodsDetailById(@PathVariable Integer id) {
        Goods goods = goodsService.getGoodsDetailById(id);
        return ResponseResult.success(goods);
    }

    /**
     * 设置商品SKU组合
     *
     * @param id 商品ID
     * @return 包含商品SKU组合列表的ResponseResult对象
     */
    @PostMapping("/{id}/set_skus_combinations")
    public ResponseResult<Boolean> setGoodsSkusAndCombinationsById(@PathVariable Integer id, @RequestBody  Goods goods) {
        Boolean re = goodsService.setGoodsSkusAndCombinationsById(id, goods);
        return ResponseResult.success(re);
    }


    // 根据数据库中某商品对应的规格及规格值来初始化该商品的规格组合初始值
    @PostMapping("/{id}/init_combinations")
    public ResponseResult<List<GoodsSkusCombinations>> setSkusCombinations(@PathVariable Integer id) {
        List<GoodsSkusCombinations> combinations = goodsSkuCombinationsService.setGoodsSkuCombinationsByGoodsId(id);
        return ResponseResult.success(combinations);
    }  
    
    /**
     * 设置商品SKU组合对应的商品信息
     *
     * @param id 商品ID
     * @return 包含商品SKU组合列表的ResponseResult对象
     */
    @PostMapping("/{id}/set_combinations_data")
    public ResponseResult<Boolean> setCombinationsDataById(@PathVariable Integer id, @RequestBody  List<GoodsSkusCombinations> combs) {
        Boolean re = goodsService.setCombinationsDataById(id, combs);
        return ResponseResult.success(re);
    }
}
